package com.ymt.bpm.web.action.admin;

import com.ymt.bpm.util.Const;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.Map;

/**
 * Created by Johnny on 2017/2/2.
 */
@Controller
public class IndexController {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private Environment env;

    @RequestMapping(path="/")
    public ModelAndView index(HttpServletRequest request) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String lang = userDetail.get(Const.LANG);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        String[] profile = env.getActiveProfiles();
        String productionVersion = env.getProperty("bpmp.production-version", "0");
        String cdn = env.getProperty("bpmp.cdn", "/bpmp-resources");
        String prefixViewpi = env.getProperty("bpmp.prefix.viewpi");
        ModelAndView view = new ModelAndView("index");
        view.addObject("profile", profile[0]);
        view.addObject("productionVersion", productionVersion);
        view.addObject("cdn", cdn);
        view.addObject(Const.LANG, lang);
        try {
            view.addObject(Const.LOGINNAME, Base64Utils.encodeToString(loginName.getBytes(Const.ENCODING)));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        view.addObject(Const.DISPLAYNAME, displayName);
        view.addObject("prefixViewpi", prefixViewpi);
        return view;
    }

    @RequestMapping(path="/login")
    public ModelAndView login(HttpServletRequest request) {
        String[] profile = env.getActiveProfiles();
        String productionVersion = env.getProperty("bpmp.production-version", "0");
        String cdn = env.getProperty("bpmp.cdn", "/bpmp-resources");

        //get redirect url
        String successUrl = "/";
        String sessionSccuessUrl = (String)request.getSession().getAttribute("successUrl");
        if (sessionSccuessUrl!=null) {
            successUrl = sessionSccuessUrl;
            log.trace("successUrl="+successUrl);
        }

        ModelAndView view = new ModelAndView("login");
        view.addObject("profile", profile[0]);
        view.addObject("productionVersion", productionVersion);
        view.addObject("cdn", cdn);
        view.addObject("authtype", env.getProperty("bpmp.auth-type"));
        view.addObject("successUrl", successUrl);
        return view;
    }

    @RequestMapping(path="/topage")
    public ModelAndView topage(HttpServletRequest request) {
        String path = request.getParameter("path");
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String lang = userDetail.get(Const.LANG);
        String[] profile = env.getActiveProfiles();
        String productionVersion = env.getProperty("bpmp.production-version", "0");
        String cdn = env.getProperty("bpmp.cdn", "/bpmp-resources");
        ModelAndView view = new ModelAndView(path);
        view.addObject("profile", profile[0]);
        view.addObject("productionVersion", productionVersion);
        view.addObject("cdn", cdn);
        view.addObject(Const.LANG, lang);
        view.addObject("fmdCtx", env.getProperty("fmd.ctx"));
        view.addObject("ctxPath", request.getContextPath());

        //redirect other parameters
        Enumeration<String> paramEnu = request.getParameterNames();
        while(paramEnu.hasMoreElements()) {
            String param = paramEnu.nextElement();
            view.addObject(param, request.getParameter(param));
        }

        return view;
    }

    /*@RequestMapping(path="/login")
    public ModelAndView login(HttpServletRequest request) {
        if (log.isDebugEnabled()) {
            log.debug("login queryString="+request.getQueryString());
        }
        return new ModelAndView("login");
    }*/

}
